﻿using System.Collections.Generic;
using SFML.Graphics;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Transformable_Engine_v2.Engine.Rendering
{
    public static class ViewHandler
    {
        private static RenderWindow _window;

        private static Dictionary<string, View> _viewDictionary;

        public static void Initialize(RenderWindow window)
        {
            _window = window;
            ClearViews();
        }

        public static void AddView(string viewName, TE_Rectangle viewArea, TE_Rectangle viewPortArea = null)
        {
            View v = new View(new FloatRect(viewArea.X * Game.Options.ScreenSize.X * Game.Options.GridSize, 
                                            viewArea.Y * Game.Options.ScreenSize.Y,
                                            (1 / viewArea.Width) * Game.Options.ScreenSize.X * Game.Options.GridSize * (viewPortArea == null ? 1 : viewPortArea.Width),
                                            (1 / viewArea.Height) * Game.Options.ScreenSize.Y * Game.Options.GridSize * (viewPortArea == null ? 1 : viewPortArea.Height)));

            if(viewPortArea != null)
                v.Viewport = viewPortArea.ToFloatRect();

            _viewDictionary.Add(viewName, v);
        }

        public static void MoveView(string viewName, TE_Vector2 position)
        {
            _viewDictionary[viewName].Center = position.ToVector2f();
        }

        public static void SetView(string viewName)
        {
            _window.SetView(_viewDictionary[viewName]);
        }

        public static void RevertToDefaultView()
        {
            _window.SetView(_window.DefaultView);
        }

        public static void ClearViews()
        {
            _viewDictionary = new Dictionary<string, View>();
        }
    }
}